package com.luckymore.ydd.app.funcs.welcome;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.base.cache.ACacheUtil;
import com.base.control.ContextControl;
import com.base.model.Bean2Json;
import com.base.util.DateUtil;
import com.base.util.FileHelper;
import com.base.util.MLog;
import com.luckymore.ydd.app.R;
import com.luckymore.ydd.app.cache.CacheUtil;
import com.luckymore.ydd.app.db.MQuestionDaoActivity;
import com.luckymore.ydd.app.funcs.login.LoginActivity;
import com.luckymore.ydd.app.funcs.mainfuncs.MainFuncsActivity;
import com.luckymore.ydd.app.funcs.util.VideoActivity;
import com.luckymore.ydd.app.model.MAppUser;
import com.luckymore.ydd.app.util.AppConfig;

/**
 * Created by yjh on 15-3-10.
 */
public class WelcomeActivity extends MQuestionDaoActivity {
    private ImageView playIv;
    private ImageView beginIv;

    private MAppUser.MUser mUser;

    //TODO bitbucket git

    @Override
    protected void create(Bundle savedInstanceState) {
        AppConfig.initApplication(this);
        setContentView(R.layout.activity_welcome);

        playIv = mFindViewById(R.id.welcome_play_video);
        beginIv = mFindViewById(R.id.welcome_begin);

        //start
        beginIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //进入登录页面，该页面退出activity栈
                ContextControl.startContext(WelcomeActivity.this, LoginActivity.class, true);
            }
        });

        mUser = ACacheUtil.getData(this, CacheUtil.USER_INFO_CACHE_KEY);
        if(mUser != null) {
            if(DateUtil.getCurrentTime("yyyy-MM-dd").equals(mUser.getCreateTime())) {
                AppConfig.isNewUserFirstDay = true;
            }
            CacheUtil.updateUserInfo(this, mUser, null);
//            if (Utils.isNetworkConnected(getMActivity())) {
//                if(!StringUtil.isEmpty(mUser.getPhone()) && !StringUtil.isEmpty(mUser.getPassword())) {
//                    AppConfig.PASSWORD = mUser.getPassword();
//                    ApiClientFactory.login(getMActivity(), getMActivity(), "loginSuccess", mUser.getPhone(), mUser.getPassword());
//                }
//            }
            ContextControl.startContext(WelcomeActivity.this, MainFuncsActivity.class, true);
        } else { //打开欢迎视频
            AppConfig.isFirst = true;

            //清除头像缓存
            try {
                FileHelper.deleteFile(AppConfig.HEAD_IMG_NAME);
            } catch (Exception e) {
                MLog.e(e);}

            openVideo();
        }

        playIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openVideo();
            }
        });
    }

    private void openVideo() {
        startActivity(new Intent(getMActivity(), VideoActivity.class));
        overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
        finish();
    }

    public void loginSuccess(Bean2Json data) {
        mUser = (MAppUser.MUser)data;
        if(mUser != null) {
            //更新缓存
            CacheUtil.updateUserInfo(this, mUser, AppConfig.PASSWORD);
        }
    }
}
